/**
 *    Copyright (c) [2021] [白晓明]
 *    [IMHarmonyOSLib] is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */
package com.itming.ui.coptlib.core;

import com.itming.ui.coptlib.enums.IMButtonIconType;
import com.itming.ui.coptlib.util.Constant;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.text.Font;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class IMIconButton extends Button {

    private static final String TAG = "IMIconButton";
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, TAG);

    public IMIconButton(Context context) {
        this(context, null);
    }

    public IMIconButton(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public IMIconButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        setTypeFace(context);
        initIMIconButton(attrSet);
    }

    private void initIMIconButton(AttrSet attrSet) {
        if (attrSet.getAttr("icon_size").isPresent()) {
            setTextSize(attrSet.getAttr("icon_size").get().getIntegerValue(), TextSizeType.FP);
        } else {
            setTextSize(Constant.DEFAULT_ICON_WIDTH_HEIGHT, TextSizeType.FP);
        }

        if (attrSet.getAttr("icon").isPresent()) {
            String icon = attrSet.getAttr("icon").get().getStringValue();
            setText(IMButtonIconType.valueOf(icon).getStyle());
        } else {
            setText(IMButtonIconType.ANGLE_LEFT.getStyle());
        }
    }

    /**
     * 设置字体显示样式 font awesome
     * @param context
     */
    private void setTypeFace(Context context) {
        String typeFace = "fontawesome-webfont.ttf";

        File file = new File(context.getCodeCacheDir(), typeFace);

        OutputStream outputStream = null;
        ResourceManager resManager = context.getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/fonts/" + typeFace);

        Resource resource = null;

        try {
            resource = rawFileEntry.openRawFile();
            outputStream = new FileOutputStream(file);
            int index;
            byte[] bytes = new byte[1024];
            if (resource != null) {
                while ((index = resource.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, index);
                    outputStream.flush();
                }
                Font font = new Font.Builder(file).build();
                setFont(font);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                resource.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
